package rsvp;

import java.io.IOException;
import java.util.ArrayList;

import junit.framework.Assert;

import org.junit.Test;

/**
 * Testing the main RSVP component
 * 
 * @author ksz
 *
 */
public class Test_DebugNow
{
	private String sTestName = null;
	private String sTextToTest = "﻿Elderly Ms. Zou Ruihuan Dies Under Persecution - Her Family of Nine Is Brutally Mistreated\r\n" + 
			"\r\n" + 
			"Name: Zou Ruihuan\r\n" + 
			"\r\n" + 
			"Gender: Female\r\n" + 
			"\r\n" + 
			"Age: 77\r\n" + 
			"\r\n" + 
			"Address: Songshan District, Chifeng City, Inner-Mongolia Autonomous Region\r\n" + 
			"\r\n" + 
			"Occupation: Unknown\r\n" + 
			"\r\n" + 
			"Date of Death: October 26th, 2009\r\n" + 
			"\r\n" + 
			"Date of Most Recent Arrest: October 30th, 2001\r\n" + 
			"\r\n" + 
			"Most Recent Place of Detention: Hongshan District Police Department in Chifeng City\r\n" + 
			"\r\n" + 
			"City: Chifeng\r\n" + 
			"\r\n" + 
			"Province: Inner Mongolia Autonomous Region\r\n" + 
			"\r\n" + 
			"Persecution Suffered: Extortion, home ransacked, interrogation, detention";

	/**
	 * @throws InterruptedException
	 * @throws IOException
	 */
	@Test public void testFindNextAndPreviousSelection() throws InterruptedException, IOException {
//		TEST_RSVP_all tAll = new TEST_RSVP_all();
//		tAll.testPaqeDown();
//		Main_RSVP applet = tAll.applet;
//		
//		sTestName = "testFindNextSelection()";
//		logInTitle(applet, "In possition");
//		
//		TextTickerThread ttt = new TextTickerThread(applet);
//		String text = applet.getTextInput().getText();
//		applet.getTextInput().requestFocus();
//		int iCurrentTextPositionChars = applet.getTextInput().getCaretPosition();
//		iCurrentTextPositionChars += applet.getTextInput().getSelectedText().length();

		// define initial conditions
		final int iStartPos = 60;
		final int iTestIterations = 4;
		final int iDisplayChunkSize = 12;
		
		int iTextPos = iStartPos;		
		ArrayList<String> sArFound = new ArrayList<String>();
		
		for(int i = 0; i < iTestIterations; i++) {
			int iNextPos = TextTickerThread.findNextSelectionEnd(sTextToTest, sTextToTest.length(), iTextPos, iDisplayChunkSize);
			final String sFound = sTextToTest.substring(iTextPos, iNextPos);
			sArFound.add(sFound);
			System.out.println("-------------------------------------------------");
			System.out.println("    iPos = " + iTextPos);
			System.out.println("iNextPos = " + iNextPos);
			System.out.println("   found = [" + sFound + "]");
			System.out.println("-------------------------------------------------");
			System.out.println();
			iTextPos = iNextPos;
		}
		
		System.out.println("-------------------------------------------------");
		System.out.println("-------------------------------------------------");
		System.out.println();
		
		for(int i = 0; i < iTestIterations; i++) {
			int iPrevPos = TextTickerThread.findPreviousSelectionEnd(sTextToTest, iTextPos, iDisplayChunkSize);
			final String sFound = sTextToTest.substring(iPrevPos, iTextPos);
			System.out.println("-------------------------------------------------");
			System.out.println("iTextPos = " + iTextPos);
			System.out.println("iPrevPos = " + iPrevPos);
			System.out.println("   found = [" + sFound + "]");
			System.out.println("-------------------------------------------------");
			System.out.println();
			Assert.assertEquals(sArFound.get(sArFound.size() - i - 1), sFound);
			iTextPos = iPrevPos;
		}
		
		
//		for (int i = 0; i < 10; i++)
//		{
//			applet.controlBigDisplay.callPageUp();
//			logInTitle(applet, "page Up called (" + i + ")");
//			//Thread.sleep(1 * 100);
//		}
		
		// wait until test is stopped with system.exit probably
		Thread.sleep(10 * 1000);

	}

	@Test public void testFindPreviousSelection() {
		
	}
	
	@Test public void testPageUp() {
		
	}
//	
//	/**
//	 * Log which stage of the test is running now
//	 * 
//	 * @param applet
//	 * @param sStage
//	 */
//	private void logInTitle(Main_RSVP applet, String sStage)
//	{
//		applet.win.setTitle(sTestName + " " + sStage);
//	}	

}
